package gov.pbc.gx.nn.pz.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

import gov.pbc.gx.nn.pz.entity.HcTzckywTzsqd;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

@Repository
public class TzckywTzsqdDao extends BaseImageDao {

	private Integer PZ_CODE_ = 3;

	public boolean isSameValue(HcTzckywTzsqd hcTzckywTzsqd) {
		String type = hcTzckywTzsqd.getType();
		String pzTemplate = hcTzckywTzsqd.getPzTemplate();
		String hql = " from HcTzckywTzsqd where deptId=:deptId and type=:type and pzTemplate=:pzTemplate and invalid=false ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		map.put("type", type);
		map.put("pzTemplate", pzTemplate);
		List<HcTzckywTzsqd> hcXjpzdatas = this.query(hql, map);
		if (!hcXjpzdatas.isEmpty()) {
			return true;
		}
		return false;
	}

	public void saveXjpz(HcTzckywTzsqd hcTzckywTzsqd) {
		if (!isSameValue(hcTzckywTzsqd)) {
			hcTzckywTzsqd.setId(null);
			// hcTzckywTzsqd.setPzDate(null);
			hcTzckywTzsqd.setDeptId(this.getDeptId());
			this.save(hcTzckywTzsqd);
		} else {
			this.update(hcTzckywTzsqd);
		}
	}

	/*
	 * 根据type，pzTemplate得到实体
	 */
	public HcTzckywTzsqd selectOneByTypeAndTemplate(String type, String pzTemplate) {
		List<HcTzckywTzsqd> HcTzckywTzsqds = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		map.put("pzTemplate", pzTemplate);
		if (type == null) {
			String hql = " from HcTzckywTzsqd where deptId=:deptId and type is null and pzTemplate=:pzTemplate and invalid = false";
			HcTzckywTzsqds = this.query(hql, map);
		} else {
			String hql = " from HcTzckywTzsqd where deptId=:deptId and type=:type and pzTemplate=:pzTemplate and invalid = false";
			map.put("type", type);
			HcTzckywTzsqds = this.query(hql, map);
		}
		if (!HcTzckywTzsqds.isEmpty()) {
			for (HcTzckywTzsqd hcTzckywTzsqd : HcTzckywTzsqds) {
				hcTzckywTzsqd.setPzDate(new Date());
			}
			return HcTzckywTzsqds.get(0);
		}
		return null;
	}

	public Collection<HcTzckywTzsqd> getCoByType(String type) {
		Collection<HcTzckywTzsqd> HcTzckywTzsqds;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		if (type == null) {
			String hql = " from HcTzckywTzsqd where deptId=:deptId and  invalid = false and type is null";

			HcTzckywTzsqds = this.query(hql, map);
		} else {
			String hql = " from HcTzckywTzsqd where deptId=:deptId and invalid = false and type=:type";
			map.put("type", type);
			HcTzckywTzsqds = this.query(hql, map);
		}
		for (HcTzckywTzsqd hcTzckywTzsqd : HcTzckywTzsqds) {
			hcTzckywTzsqd.setPzDate(new Date());
		}
		return HcTzckywTzsqds;
	}

	public Collection<HcTzckywTzsqd> getOneGroupByType() {
		String hql = " from HcTzckywTzsqd where deptId=:deptId and invalid = false group by type";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deptId", this.getDeptId());
		Collection<HcTzckywTzsqd> hcTzckywTzsqd = this.query(hql, param);
		for (HcTzckywTzsqd hcTzckywTzsqd2 : hcTzckywTzsqd) {
			hcTzckywTzsqd2.setPzDate(new Date());
		}
		return hcTzckywTzsqd;
	}

	public Collection<HcTzckywTzsqd> getMuByDeptName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", this.getDeptId());
		String hql = "from HcTzckywTzsqd where deptId=:deptId and invalid=false order by pzTemplate ";
		Collection<HcTzckywTzsqd> hcTzckywTzsqd = query(hql, map);
		for (HcTzckywTzsqd hcTzckywTzsqd2 : hcTzckywTzsqd) {
			hcTzckywTzsqd2.setPzDate(new Date());
		}
		return hcTzckywTzsqd;
	}

	public List<HcpzXY> getHcPzXYs(Integer pzcode) {
		List<HcpzXY> lists = getHcPzXYsBase(pzcode);
		return lists;
	}

	public void saveXjpzXY(Collection<HcpzXY> hcxjpzxys) {
		saveXjpzXYBase(hcxjpzxys, PZ_CODE_);
	}

	public void updateFont(PzFont pzfont) {
		updateFontBase(pzfont, PZ_CODE_);
	}

	public void deleteTemplateById(String id) {
		this.delete(id, HcTzckywTzsqd.class);
	}

	/**
	 * 更新模板名稱
	 * 
	 * @auther jack.zhao
	 * @param templatename
	 * @param id
	 * @return
	 */
	public boolean updateTemplateNameDao(final String templatename, final String id) {

		// 根據id得到type
		String sql1 = "from HcTzckywTzsqd where id=:id and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		List<HcTzckywTzsqd> hcTzckywTzsqds1 = query(sql1, param);
		String typestr = hcTzckywTzsqds1.get(0).getType();

		// 根據type查找所有的pztemplate是否用相同的，
		String sql = "from HcTzckywTzsqd where  invalid = false and type=:type ";
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("type", typestr);

		Collection<HcTzckywTzsqd> hcTzckywTzsqds = this.query(sql, param1);
		for (HcTzckywTzsqd hcTzckywTzsqd : hcTzckywTzsqds) {
			if (hcTzckywTzsqd.getPzTemplate().equals(templatename)) {
				return false;
			}
		}
		// 更新操作
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			String hql = "update HcTzckywTzsqd set pzTemplate=:pzTemplate  where id=:id ";

			@Override
			public Boolean doInSession(Session session) {
				Query query = session.createQuery(hql);
				query.setString("pzTemplate", templatename);
				query.setString("id", id);
				query.executeUpdate();
				return true;
			}

		});

	}

	/**
	 * 更新模板類型
	 * 
	 * @auther jack.zhao
	 * @param templatetype
	 * @param id
	 * @return
	 */
	public boolean updateTemplateTypeDao(final String templatetype, final String id) {
		// 判斷類型是否有相同的
		String sql = "select count(*) from HcTzckywTzsqd where type=:type and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", templatetype);
		int count = this.queryForInt(sql, param);
		if (count > 0) {
			return false;
		}

		// 更新類型
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			@Override
			public Boolean doInSession(Session session) {

				String sql = "from HcTzckywTzsqd where id=:id and invalid = false ";
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", id);
				List<HcTzckywTzsqd> hcTzckywTzsqds = query(sql, param);
				String typestr = hcTzckywTzsqds.get(0).getType();

				String sql1 = "from HcTzckywTzsqd where type=:type and invalid = false ";
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("type", typestr);
				List<HcTzckywTzsqd> hcTzckywTzsqds1 = query(sql1, param1);
				for (HcTzckywTzsqd tzsqd : hcTzckywTzsqds1) {
					String hql2 = "update HcTzckywTzsqd set type=:type  where id=:id ";
					Query query = session.createQuery(hql2);
					query.setString("type", templatetype);
					query.setString("id", tzsqd.getId());
					query.executeUpdate();
				}

				return true;
			}

		});

	}

}
